Repository navigation
London | 26-ITP-Sep | Shirin Panahian | Sprint 1 | Complete sprint 1 exercises - #1498
shirinpanahian wants to merge 14 commits into
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
cjyuan
left a comment
There was a problem hiding this comment.
-
Function implementations are all correct. Well done.
-
Regarding the PR description, could you use AI to research what should be included in the "Changelist" section of a PR, then revise your Changelist content accordingly?
| } | ||
|
|
||
| for (const item of list) { | ||
| if (typeof item !== "number" || !Number.isFinite(item)) { |
There was a problem hiding this comment.
Very thoughtful to take Infinity and NaN into consideration, but one of these checks is redundant.
There was a problem hiding this comment.
Thanks for the feedback! You're right. I've removed the redundant check and kept Number.isFinite().
| const copy = [...list]; | ||
| const middleIndex = Math.floor(listLength / 2); | ||
| copy.sort((a, b) => a - b); |
There was a problem hiding this comment.
Could also use Array.prototype.toSorted().
| if (newList.includes(item)) { | ||
| continue; | ||
| } else { | ||
| newList.push(item); | ||
| } |
There was a problem hiding this comment.
Note: Code on lines 7-11 could also be expressed without using else.
There was a problem hiding this comment.
Thanks for the suggestion! I've simplified the code by removing the unnecessary else.
| // Given an array with no duplicates | ||
| // When passed to the dedupe function | ||
| // Then it should return a copy of the original array | ||
| it("Given an array with no duplicates, it returns a copy of the original array", () => { | ||
| expect(dedupe(["d", "s", "t", "c"])).toEqual(["d", "s", "t", "c"]); | ||
| expect(dedupe([5, 4, 3, 2, 1])).toEqual([5, 4, 3, 2, 1]); | ||
| expect(dedupe([7, 8, 9])).toEqual([7, 8, 9]); | ||
| }); |
There was a problem hiding this comment.
Your function implementation is correct. However, this test could be improved to better ensure
that any future changes continue to align with the expected behavior described on line 32:
Then it should return a copy of the original array
This test should fail if the function returns the original array (instead of a copy of the original array).
The current tests check only if both the original array and the returned array contain identical elements.
In order to validate the returned array is a different array, we need an additional check.
Could you find out what this additional check is, and then implement it for one of the test cases?
There was a problem hiding this comment.
Thanks for the suggestion! I've added an additional check using not.toBe() to ensure the function returns a new array rather than the original one.
| test.todo("given an empty array, returns -Infinity"); | ||
| describe("findMax", () => { | ||
| it("given an empty array,returns -Infinity", () => { | ||
| expect(findMax([])).toEqual([-Infinity]); | ||
| }); |
There was a problem hiding this comment.
The expected return value is a value of type "number" and not an array.
There was a problem hiding this comment.
Thanks! I've made the suggested changes.
| for (const item of elements) { | ||
| if (maxNum < item) { | ||
| maxNum = item; | ||
| } | ||
| } |
There was a problem hiding this comment.
Note: This for loop alone could handle an array of any size if you initialise maxNum to the smallest possible value.
There was a problem hiding this comment.
Thanks for the suggestion! I've updated the initial value to -Infinity.
There was a problem hiding this comment.
How should the function handle an array containing NaN?
There was a problem hiding this comment.
Arrays containing NaN should throw an error. I've handled this using Number.isFinite().
There was a problem hiding this comment.
What should calculateMean([Infinity, 0, -Infinity]) or calculateMean([0, NaN]) return?
The same also applies to sum().
There was a problem hiding this comment.
Thanks! I've updated the validation to reject non-finite numbers and added tests.
| sum = sum + item; | ||
| }); | ||
| mean = sum / list.length; | ||
| return Number(mean.toFixed(2)); |
There was a problem hiding this comment.
Why reduce the precision of the result?
There was a problem hiding this comment.
The tests expect results rounded to 2 decimal places, so I've kept the rounding to match the expected behaviour.
| it("given an array with negative or decimal numbers, returns the correct mean", () => { | ||
| expect(calculateMean([-2, -6, -76])).toEqual(-28); | ||
| expect(calculateMean([10, 0, -11])).toEqual(-0.33); | ||
| expect(calculateMean([6.87, 3.12, -11.25])).toEqual(-0.42); | ||
| }); |
There was a problem hiding this comment.
Decimal numbers in most programming languages (including JS) are internally represented in "floating point number" format. Floating point arithmetic is not exact. For example, the result of 46.5678 - 46 === 0.5678 is false because 46.5678 - 46 only yield a value that is very close to 0.5678. Even changing the order in which the program add/subtract numbers can yield different values.
So the following could happen
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.805 ); // This fail
expect( 1.2 + 0.6 + 0.005 ).toEqual( 1.8049999999999997 ); // This pass
expect( 0.005 + 0.6 + 1.2 ).toEqual( 1.8049999999999997 ); // This fail
console.log(1.2 + 0.6 + 0.005 == 1.805); // false
console.log(1.2 + 0.6 + 0.005 == 0.005 + 0.6 + 1.2); // falseCan you find a more appropriate way to test a value (that involves decimal number calculations) for equality?
Suggestion: Look up
- Checking equality in floating point arithmetic in JavaScript
- Checking equality in floating point arithmetic with Jest
There was a problem hiding this comment.
Thanks for the explanation! I've updated the decimal number tests to use Jest's toBeCloseTo().
Learners, PR Template
Self checklist
Task code
CYF-1071
Changelist
Changelist
Implemented
dedupe: Removes duplicate elements from an array while preserving the original order and first occurrence of each element. Handles empty arrays and arrays without duplicates.Implemented
findMax: Finds and returns the largest number in an array, with handling for empty arrays, single-element arrays and invalid inputs.Implemented
sumandmean: Added the functions to calculate the sum and arithmetic mean of an array of numbers.Implemented
calculateMedian: Calculates the median of an array of numbers, supporting both odd and even numbers of elements. Includes input validation and error handling.Implemented
describeMedian: Returns a descriptive message containing the calculated median, or an appropriate error message when the median cannot be calculated.Refactored
includes: Updated the implementation to use afor...ofloop.Added and updated tests: Expanded test coverage for normal cases, empty arrays, single-element arrays, duplicate elements and invalid inputs.